﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Web.UI.WebControls;
using ConsolidationTool.Common;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace ConsolidationTool.WebParts.Layouts.CReport.Actual
{
    public partial class ARAP : FSReportBase
    {

        public List<RQDataItem> TableData = new List<RQDataItem>();
        public override Repeater BaseReport
        {
            get
            {
                return EQRepeater;
            }
        }
        public override CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }
        public override void BeforLoad()
        {
            base.BeforLoad();

            //if (!IsPostBack)
            //    loadConfigYearMonth();

            //InitBaseInfo();
        }

        CacheSPItem currentMonthItem = null;
        CacheSPItem lastMonthItem = null;
        CacheSPItem lastYearMonthItem = null;
        CacheSPItem lastYearDecItem = null;

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            currentMonthItem = new CacheSPItem(BusinessService.GetItem(CEntity.Entity, CEntity.Year, CEntity.Month, "Actual_ARAP"));
            lastMonthItem = new CacheSPItem(BusinessService.GetItem(CEntity.Entity, CEntity.SelectedDateTime.AddMonths(-1).Year, CEntity.SelectedDateTime.AddMonths(-1).Month, "Actual_ARAP"));
            lastYearMonthItem = new CacheSPItem(BusinessService.GetItem(CEntity.Entity, CEntity.Year - 1, CEntity.Month, "Actual_ARAP"));
            lastYearDecItem = new CacheSPItem(BusinessService.GetItem(CEntity.Entity, CEntity.Year - 1, 12, "Actual_ARAP"));

            BindDataMonth();

            BaseReport.DataSource = TableData;//.OrderBy(p => p.AccountCode);
            BaseReport.DataBind();
        }

        public void BindDataMonth()
        {
            if (currentMonthItem.KoFields == null) return;
            //  int currentIndex = 0;
            currentMonthItem.KoFields.Where(p => !p.controlrow).ToList().ForEach(p =>
            {
                if (p.emptyrow)
                    TableData.Add(new RQDataItem());
                else
                {
                    var instance = CreateKoInstance(p, false, 0);

                    if (p.subrow != null)
                    {
                        // var parentNode = TableData.FirstOrDefault(c => c.AccountCode == instance.AccountCode);
                        int index = TableData.IndexOf(instance) + 1;
                        p.subrow.ForEach(a =>
                        {
                            CreateKoInstance(a, true, index);
                        });
                    }
                }

                // currentIndex++;
            });


        }

        private RQDataItem CreateKoInstance(BaseKoOutputEntity p, bool insert, int index)
        {
            bool fieldExist = true;

            var subRowInstance = new koSubRowOutputEntity();

            var ins = TableData.FirstOrDefault(a => a.acountcode == p.accountcode);

            var n_1_value = lastYearMonthItem["CM_" + p.accountcode];
            var lastYearDec_value = lastYearDecItem["CM_" + p.accountcode];
            var lastMonthValue = lastMonthItem["CM_" + p.accountcode];

            if (ins == null)
            {
                fieldExist = false;
                ins = new RQDataItem();
                ins.acountcode = p.accountcode;
                ins.title = p.description;
                ins.unit = p.unit;

                ins.values.Add(n_1_value);
                ins.values.Add(lastYearDec_value);
                ins.values.Add(lastMonthValue);
                ins.values.Add(p.CM);
                ins.values.Add("");
                ins.values.Add(p.ND);
                ins.values.Add(p.D15);
                ins.values.Add(p.D30);
                ins.values.Add(p.D60);
                ins.values.Add(p.D90);
                ins.values.Add(p.D150);
                ins.values.Add(p.G150);
            }
            if (p is koSubRowOutputEntity)
            {
                subRowInstance = p as koSubRowOutputEntity;
                ins.title = subRowInstance.entity;
            }



            if (!fieldExist)
            {
                if (p.bold)
                    ins.title = string.Format("<strong>{0}</strong>", ins.title);

                if (insert)
                    TableData.Insert(index, ins);
                else
                    TableData.Add(ins);
            }

            return ins;
        }

    }
}
